package com.gc.controller.courseServlet;

import com.gc.entity.Course;
import com.gc.repository.CourseRepository;
import com.gc.repository.impl.CourseRepositoryImpl;
import com.gc.util.result.R;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "courseUpdateById", value = "/courseUpdateById" )
public class courseUpdateById extends HttpServlet {
    CourseRepository courseRepository = new CourseRepositoryImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        Course course = new Course(Long.valueOf(request.getParameter("id"))
                 ,request.getParameter("name"),
                request.getParameter("cid"));
        boolean res = courseRepository.UpdateById(course);
        if(res) {
            if(res) {
                response.getWriter().write(gson.toJson(R.ok()));
            } else {
                response.getWriter().write(gson.toJson(R.error()));
            }
        }
    }
}
